﻿// -----------------------------------------------------------------------
// <copyright file="DmVatTuDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmVatTuDataAccess
    {
        public bool InsertDmVatTu(DmVatTuEntity obj)
        {
            try
            {
                string sql = "spDmVatTu_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaVatTu", DbType.String, obj.MaVatTu);
                db.AddInParameter(cmd, "TenVatTu", DbType.String, obj.TenVatTu);
                db.AddInParameter(cmd, "TenGoc", DbType.String, obj.TenGoc);
                db.AddInParameter(cmd, "MaDonVi", DbType.String, obj.MaDonVi);
                db.AddInParameter(cmd, "ThanhPhan", DbType.String, obj.ThanhPhan);
                db.AddInParameter(cmd, "MaLoaiVatTu", DbType.String, obj.MaLoaiVatTu);
                db.AddInParameter(cmd, "MaPhanLoaiVatTu", DbType.String, obj.MaPhanLoaiVatTu);
                db.AddInParameter(cmd, "MaHangSanXuat", DbType.String, obj.MaHangSanXuat);
                db.AddInParameter(cmd, "MaNhomVatTu", DbType.String, obj.MaNhomVatTu);
                db.AddInParameter(cmd, "HuongDanSuDung", DbType.String, obj.HuongDanSuDung);
                db.AddInParameter(cmd, "HamLuong", DbType.String, obj.HamLuong);
                db.AddInParameter(cmd, "MaCachDung", DbType.String, obj.MaCachDung);
                db.AddInParameter(cmd, "MaDonViDung", DbType.String, obj.MaDonViDung);
                db.AddInParameter(cmd, "LanDung", DbType.Double, obj.LanDung);
                db.AddInParameter(cmd, "SoLuongDung", DbType.Double, obj.SoLuongDung);
                db.AddInParameter(cmd, "MaGhiChuCachDung", DbType.String, obj.MaGhiChuCachDung);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmVatTu", ex.Message); throw ex; }
        }

        public bool EditDmVatTu(DmVatTuEntity obj)
        {
            try
            {
                string sql = "spDmVatTu_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaVatTu", DbType.String, obj.MaVatTu);
                db.AddInParameter(cmd, "TenVatTu", DbType.String, obj.TenVatTu);
                db.AddInParameter(cmd, "TenGoc", DbType.String, obj.TenGoc);
                db.AddInParameter(cmd, "MaDonVi", DbType.String, obj.MaDonVi);
                db.AddInParameter(cmd, "ThanhPhan", DbType.String, obj.ThanhPhan);
                db.AddInParameter(cmd, "MaLoaiVatTu", DbType.String, obj.MaLoaiVatTu);
                db.AddInParameter(cmd, "MaPhanLoaiVatTu", DbType.String, obj.MaPhanLoaiVatTu);
                db.AddInParameter(cmd, "MaHangSanXuat", DbType.String, obj.MaHangSanXuat);
                db.AddInParameter(cmd, "MaNhomVatTu", DbType.String, obj.MaNhomVatTu);
                db.AddInParameter(cmd, "HuongDanSuDung", DbType.String, obj.HuongDanSuDung);
                db.AddInParameter(cmd, "HamLuong", DbType.String, obj.HamLuong);
                db.AddInParameter(cmd, "MaCachDung", DbType.String, obj.MaCachDung);
                db.AddInParameter(cmd, "MaDonViDung", DbType.String, obj.MaDonViDung);
                db.AddInParameter(cmd, "LanDung", DbType.Double, obj.LanDung);
                db.AddInParameter(cmd, "SoLuongDung", DbType.Double, obj.SoLuongDung);
                db.AddInParameter(cmd, "MaGhiChuCachDung", DbType.String, obj.MaGhiChuCachDung);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmVatTu", ex.Message); throw ex; }
        }

        public bool DeleteDmVatTu(string maVatTu)
        {
            try
            {
                string sql = "spDmVatTu_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaVatTu", DbType.String, maVatTu);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmVatTu", ex.Message); throw ex; }
        }

        public DmVatTuEntity GetDmVatTu(string maVatTu)
        {
            try
            {
                DmVatTuEntity obj = null;
                string sql = "spDmVatTu_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaVatTu", DbType.String, maVatTu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmVatTuEntity();
                        obj.MaVatTu = reader["MaVatTu"].ToString();
                        obj.TenVatTu = reader["TenVatTu"].ToString();
                        obj.TenGoc = reader["TenGoc"].ToString();
                        obj.MaDonVi = reader["MaDonVi"].ToString();
                        obj.ThanhPhan = reader["ThanhPhan"].ToString();
                        obj.MaLoaiVatTu = reader["MaLoaiVatTu"].ToString();
                        obj.MaPhanLoaiVatTu = reader["MaPhanLoaiVatTu"].ToString();
                        obj.TenPhanLoaiVatTu = reader["TenPhanLoaiVatTu"].ToString();
                        obj.MaHangSanXuat = reader["MaHangSanXuat"].ToString();
                        obj.TenHang = reader["TenHang"].ToString();
                        obj.MaNhomVatTu = reader["MaNhomVatTu"].ToString();
                        obj.TenNhomVatTu = reader["TenNhomVatTu"].ToString();
                        obj.HuongDanSuDung = reader["HuongDanSuDung"].ToString();
                        obj.HamLuong = reader["HamLuong"].ToString();
                        obj.MaCachDung = reader["MaCachDung"].ToString();
                        obj.MaDonViDung = reader["MaDonViDung"].ToString();
                        obj.LanDung = String.IsNullOrEmpty(reader["LanDung"].ToString()) == true ? 0 : (double)reader["LanDung"];
                        obj.SoLuongDung = String.IsNullOrEmpty(reader["SoLuongDung"].ToString()) == true ? 0 : (double)reader["SoLuongDung"];
                        obj.MaGhiChuCachDung = reader["MaGhiChuCachDung"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmVatTu", ex.Message); throw ex; }
        }

        public List<DmVatTuEntity> GetDmVatTuList(string maPhanLoaiVatTu)
        {
            try
            {
                List<DmVatTuEntity> list = new List<DmVatTuEntity>();
                DmVatTuEntity obj = null;
                string sql = "spDmVatTu_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaPhanLoaiVatTu", DbType.String, maPhanLoaiVatTu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmVatTuEntity();
                        obj.MaVatTu = reader["MaVatTu"].ToString();
                        obj.TenVatTu = reader["TenVatTu"].ToString();
                        obj.TenGoc = reader["TenGoc"].ToString();
                        obj.MaDonVi = reader["MaDonVi"].ToString();
                        obj.ThanhPhan = reader["ThanhPhan"].ToString();
                        obj.MaLoaiVatTu = reader["MaLoaiVatTu"].ToString();
                        obj.MaPhanLoaiVatTu = reader["MaPhanLoaiVatTu"].ToString();
                        obj.TenPhanLoaiVatTu = reader["TenPhanLoaiVatTu"].ToString();
                        obj.MaHangSanXuat = reader["MaHangSanXuat"].ToString();
                        obj.TenHang = reader["TenHang"].ToString();
                        obj.MaNhomVatTu = reader["MaNhomVatTu"].ToString();
                        obj.TenNhomVatTu = reader["TenNhomVatTu"].ToString();
                        obj.HuongDanSuDung = reader["HuongDanSuDung"].ToString();
                        obj.HamLuong = reader["HamLuong"].ToString();
                        obj.MaCachDung = reader["MaCachDung"].ToString();
                        obj.MaDonViDung = reader["MaDonViDung"].ToString();
                        obj.LanDung = String.IsNullOrEmpty(reader["LanDung"].ToString()) == true ? 0 : (double)reader["LanDung"];
                        obj.SoLuongDung = String.IsNullOrEmpty(reader["SoLuongDung"].ToString()) == true ? 0 : (double)reader["SoLuongDung"];
                        obj.MaGhiChuCachDung = reader["MaGhiChuCachDung"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmVatTuList", ex.Message); throw ex; }
        }

    }
}